function dispatchAll(url, message, update) {
  var x = new XMLHttpRequest();
  x.open("POST", url, true);
  x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  x.onreadystatechange = function() {
    switch(x.readyState) {
      case 4:
      update(x);
      break;
    }
  }
  x.send(message);
}


function convertRequest(request, update) {
  dispatchAll("/pkcs/PKCSToXMLDSig", 'signature='+encodeURIComponent(request), update);
}

function CryptoObject() {
  var self = this;
  var signText;
  var response;
  var ecb;

  this.toSign = function() {
    throw new Error("Object must overload toSign() method!");
  }

  this.setECB = function(x) { ecb = x; }

  this.sign = function(cb) {
    self.signText = crypto.signText(self.toSign(), "auto");
    if('error:' == self.signText.substring(0, 6))
      throw new Error(self.signText.substring(6));
    convertRequest(self.signText, function(x) {
		     if(x.status >= 400) {
		       if(ecb)
	                       ecb("Signature conversion failed", "signature_conversion");
                       return;
                     }
		     self.response = x;
		     cb(self);
		   });

  };
}

function Dummy() {
  this.CryptoObject = CryptoObject;
  this.CryptoObject();
  this.toSign = function() { return "Just a test" };
}
